import requests

from api import log
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from config import HOST
from tools import common_assert, html_parser


class TestApproveTrust:
    # 初始化
    def setup_method(self):
        # 获取session对象
        log.info("正在获取session对象")
        self.session = requests.session()
        # 获取ApiApprove_trust对象
        log.info("正在获取ApiApprove_trust对象")
        self.api = ApiApproveTrust(self.session)
        # 获取ApiRegisterLogin实例对象
        log.info("正在获取ApiRegisterLogin对象")
        self.login = ApiRegisterLogin(self.session)
        # self.api_login = ApiRegisterLogin(self.session).api_login()

    # 结束
    def teardown_method(self):
        # 关闭session对象
        log.info("正在关闭session对象")
        self.session.close()

    # 1、调用认证接口 测试
    def test01_approve(self, realname="张三", card_id=110101201703072050, expect_json="提交成功"):
        try:
            # 1、登录
            self.login.api_login()
            # 2、认证
            result = self.api.api_approve(realname=realname, card_id=card_id)
            log.info("认证接口执行结果为:{}".format(result.text))
            # 断言
            common_assert(result, expect_json=expect_json)
            log.info("认证接口断言通过")
        except Exception as e:
            log.error(e)
            raise

    # 2、调用认证查询状态接口 测试
    def test02_approve_status(self, expect_text="1111"):
        try:
            # 1、登录
            self.login.api_login()
            # 2、认证查询
            result = self.api.api_approve_status()
            log.info("认证查询接口执行结果为:{}".format(result.text))
            # 断言
            common_assert(result, expect_text=expect_text)
            log.info("认证查询接口断言通过")
        except Exception as e:
            log.error(e)
            raise

    # 3、调用后台开户接口 测试
    def test03_trust(self, expect_text="form", expect_text2="OK"):
        try:
            # 1、登录
            self.login.api_login()
            # 2、后台开户
            result = self.api.api_trust()
            log.info("后台开户接口执行结果为:{}".format(result.json()))
            # 认证接口 断言
            common_assert(result, expect_text=expect_text)
            log.info("后台开户接口断言通过")
            # 三方开户实现
            data = html_parser(result)
            result2 = self.session.post(url=data[0], data=data[1])
            log.info("三方开户接口执行结果为:{}".format(result2.text))
            # 三方开户接口 断言
            common_assert(result2, expect_text=expect_text2)
            log.info("三方开户接口断言通过")
        except Exception as e:
            log.error(e)
            raise
